#include "BDataView.h"
#include "ui_BDataView.h"

BDataView::BDataView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BDataView)
{
    ui->setupUi(this);
    setDatabase();

    initModeSelect();
}

BDataView::~BDataView()
{
    delete ui;
}

void BDataView::setPageIndex(int index)
{
    ui->widgetModeSelect->changePage(index);
}

void BDataView::initModeSelect()
{
    SelectItem* temperature = new SelectItem(":/icons/Icon/database-100.png");
    SelectItem* charge = new SelectItem(":/icons/Icon/warning-100.png");
    ui->widgetModeSelect->addItem(temperature);
    ui->widgetModeSelect->addItem(charge);
    connect(ui->widgetModeSelect, &ModeSelectWidget::itemChange,
            this, [&](int indexID){
       ui->stackedWidgetData->setCurrentIndex(indexID);
    });
    emit ui->widgetModeSelect->itemChange(0);
}

void BDataView::setDatabase()
{
    //1.使用globalDB初始化
    //2.打开表
    openTabel();
}

void BDataView::openTabel()
{
    //1.初始化table Model
    m_tableModel = new QSqlRelationalTableModel(this, globalDB);
    //2.打开表并查询
    m_tableModel->setTable("test2");
    m_tableModel->select();
    //3.设置编辑策略 和 排序
    m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_tableModel->setSort(0, Qt::SortOrder::DescendingOrder);    //升序
    //4.设置数据库显示名称
    m_tableModel->setHeaderData(m_tableModel->fieldIndex("tro_num"), Qt::Horizontal, "tro_num");
    m_tableModel->setHeaderData(m_tableModel->fieldIndex("tro_net"), Qt::Horizontal, "tro_net");
    m_tableModel->setHeaderData(m_tableModel->fieldIndex("x_pos"), Qt::Horizontal, "x_pos");
    m_tableModel->setHeaderData(m_tableModel->fieldIndex("y_pos"), Qt::Horizontal, "y_pos");
    m_tableModel->setHeaderData(m_tableModel->fieldIndex("theta_pos"), Qt::Horizontal, "theta_pos");
    m_tableModel->setHeaderData(m_tableModel->fieldIndex("p"), Qt::Horizontal, "p");
    m_tableModel->setHeaderData(m_tableModel->fieldIndex("v"), Qt::Horizontal, "v");
    m_tableModel->setHeaderData(m_tableModel->fieldIndex("wv"), Qt::Horizontal, "wv");
    m_tableModel->setHeaderData(m_tableModel->fieldIndex("tro_ele"), Qt::Horizontal, "tro_ele");
    m_tableModel->setHeaderData(m_tableModel->fieldIndex("tro_sta"), Qt::Horizontal, "tro_sta");
    m_tableModel->setHeaderData(m_tableModel->fieldIndex("tro_load"), Qt::Horizontal, "tro_load");
    //5.初始化选择模型
    m_selection = new QItemSelectionModel(m_tableModel);
    //6.显示
    ui->tableView->setModel(m_tableModel);
    ui->tableView->setSelectionModel(m_selection);
//    ui->tableView->setColumnHidden(m_tableModel->fieldIndex("number"), true);
//    ui->tableView->setColumnWidth(m_tableModel->fieldIndex("id"), 250);
//    ui->tableView->setColumnWidth(m_tableModel->fieldIndex("time"), 200);

    //7.初始化过滤器
//    initComboxFilter();
}

void BDataView::initComboxFilter()
{
//    //title过滤
//    m_filter[0] = "测试柜";
//    m_filter[1] = "节点0";
//    //nodeName过滤
//    ui->filterNodeName->addItems(m_nodeName);
//    ui->filterNodeName->addItem("全部显示");
    connect(ui->filterNodeName, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged),
            this, [&](const QString& filter){
        m_filter[1] = filter;
        emit startFilter();
    });
    connect(this, &BDataView::startFilter, [&](){
        m_tableModel->setFilter(tr("(title = '%1') AND (nodeName = '%2')").arg(m_filter[0]).arg(m_filter[1]));
    });
    //触发初始过滤
    emit startFilter();
}

QString BDataView::getDateTimeCurrent()
{
    QDateTime dateTime= QDateTime::currentDateTime();//获取系统当前的时间
    QString str = dateTime .toString("yyyy-MM-dd hh:mm:ss");//格式化时间
    return str;
}

QDateTime BDataView::getDateTimeFromString(const QString &dateTime)
{
    QDateTime time = QDateTime::fromString(dateTime, "yyyy-MM-dd hh:mm:ss");
    return time;
}

void BDataView::setQueryDateTime(const QDateTime &startDateTime, const QDateTime &endDateTime)
{
//    qDebug() << "startDateTime:" << startDateTime << endl << "endDateTime:" << endDateTime;
    assert( ("开始时间小于结束时间" && startDateTime < endDateTime) );    //设置断言，起始时间应该小于结束时间
    m_startDateTime = startDateTime;
    m_endDateTime = endDateTime;
}

void BDataView::setQueryDateTime(const QString &startDateTime, const QString &endDateTime)
{
    QDateTime start = getDateTimeFromString(startDateTime);
    QDateTime end = getDateTimeFromString(endDateTime);
    setQueryDateTime(start, end);
}
